package org.jeecg.modules.wlhy.enterprise.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.wlhy.enterprise.entity.HyEnterprise;
import org.jeecg.modules.wlhy.enterprise.service.IHyEnterpriseService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 企业表
 * @Author: jeecg-boot
 * @Date:   2020-12-02
 * @Version: V1.0
 */
@Api(tags="企业表")
@RestController
@RequestMapping("/enterprise/hyEnterprise")
//@RequiresRoles(value={"admin", "customer", "driver"},logical = Logical.OR)
@Slf4j
public class HyEnterpriseController extends JeecgController<HyEnterprise, IHyEnterpriseService> {
	@Autowired
	private IHyEnterpriseService hyEnterpriseService;

	/**
	 * 分页列表查询
	 *
	 * @param hyEnterprise
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */

	@ApiOperation(value="企业表-分页列表查询", notes="企业表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyEnterprise hyEnterprise,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HyEnterprise> queryWrapper = QueryGenerator.initQueryWrapper(hyEnterprise, req.getParameterMap());
		Page<HyEnterprise> page = new Page<HyEnterprise>(pageNo, pageSize);
		IPage<HyEnterprise> pageList = hyEnterpriseService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param hyEnterprise
	 * @return
	 */
	@AutoLog(value = "企业表-添加")
	@ApiOperation(value="企业表-添加", notes="企业表-添加")
	@PostMapping(value = "/add")
//	@RequiresPermissions("admin:enterprise:add")
	public Result<?> add(@RequestBody HyEnterprise hyEnterprise) {
		hyEnterpriseService.save(hyEnterprise);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param hyEnterprise
	 * @return
	 */
	@AutoLog(value = "企业表-编辑")
	@ApiOperation(value="企业表-编辑", notes="企业表-编辑")
	@PutMapping(value = "/edit")
//	@RequiresPermissions("admin:enterprise:edit")
	public Result<?> edit(@RequestBody HyEnterprise hyEnterprise) {
		hyEnterpriseService.updateById(hyEnterprise);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "企业表-通过id删除")
	@ApiOperation(value="企业表-通过id删除", notes="企业表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		hyEnterpriseService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "企业表-批量删除")
	@ApiOperation(value="企业表-批量删除", notes="企业表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hyEnterpriseService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="企业表-通过id查询", notes="企业表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		HyEnterprise hyEnterprise = hyEnterpriseService.getById(id);
		if(hyEnterprise==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hyEnterprise);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hyEnterprise
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HyEnterprise hyEnterprise) {
        return super.exportXls(request, hyEnterprise, HyEnterprise.class, "企业表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HyEnterprise.class);
    }


	 /**
	  * 查询表记录数
	  *
	  * @return
	  */
//	 @AutoLog(value = "企业表-查询表记录数")
	 @ApiOperation(value="企业表-查询表记录数", notes="企业表-查询表记录数")
	 @GetMapping(value = "/count")
	 public int count() {
		 return hyEnterpriseService.count();
	 }

	 /**
	  * 查询表中第一条数据
	  *
	  * @return
	  */
//	 @AutoLog(value = "企业表-查询表中第一条数据")
	 @ApiOperation(value="企业表-查询表中第一条数据", notes="企业表-查询表中第一条数据")
	 @GetMapping(value = "/queryTopData")
	 public Result<?> queryTopData() {
		 HyEnterprise hyEnterprise = hyEnterpriseService.queryTopData();
		 if(hyEnterprise == null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(hyEnterprise);
	 }
}
